package com.hepinghospital.user.ui.activity.prescription;

import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.util.GlideUtil;
import com.hepinghospital.user.util.ImageLoaderHelper;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.response.Photo;
import com.rest.response.PrescriptionMedhisDto;
import com.rest.response.PrescriptionResponse;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Description
 * Created by lbj on 2021/8/26 16:32
 */

public class AtyHistoryCaseActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_hospita_name)
    TextView tvHospitaName;
    @BindView(R.id.tv_dept_name)
    TextView tvDeptName;
    @BindView(R.id.tv_id_card)
    TextView tvIdCard;
    @BindView(R.id.tv_patient_name)
    TextView tvPatientName;
    @BindView(R.id.tv_patient_sex)
    TextView tvPatientSex;
    @BindView(R.id.tv_patient_age)
    TextView tvPatientAge;
    @BindView(R.id.tv_medhiszs)
    TextView tvMedhiszs;
    @BindView(R.id.rv_photos)
    RecyclerView rvPhotos;
    @BindView(R.id.tv_medhisxbs)
    TextView tvMedhisxbs;
    @BindView(R.id.tv_medhisjws)
    TextView tvMedhisjws;
    @BindView(R.id.tv_eyespecial)
    TextView tvEyespecial;
    @BindView(R.id.tv_diagnames)
    TextView tvDiagnames;
    @BindView(R.id.tv_diagnoseadvice)
    TextView tvDiagnoseadvice;
    @BindView(R.id.iv_sign)
    ImageView ivSign;
    @BindView(R.id.tv_time)
    TextView tvTime;
    private String diagnoseId, orderId, receiverAddress, receiverPhone;
    List<PrescriptionResponse.Prescription> list = new ArrayList<>();
    MyAdapter adapter;
    List<Photo> photoList = new ArrayList<>();

    @Override
    protected int getLayoutResource() {
        return R.layout.aty_history_case;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initViews() {
        tvTitle.setText("我的病历");
        GridLayoutManager manager = new GridLayoutManager(this, 3);
        rvPhotos.setLayoutManager(manager);
        adapter = new MyAdapter();
        rvPhotos.setNestedScrollingEnabled(false);
        rvPhotos.setAdapter(adapter);
        rvPhotos.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(@NotNull Canvas c, @NotNull RecyclerView parent, @NotNull RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(@NotNull Rect outRect, @NotNull View view, @NotNull RecyclerView parent, @NotNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = 1;
            }
        });
    }

    @Override
    protected void initData() {
        diagnoseId = getIntent().getStringExtra("diagnoseId");
        getPrescriptionMedhisDtoByIdApp(diagnoseId);
    }


    private void getPrescriptionMedhisDtoByIdApp(String diagnoseId) {
        RestProxy.getInstance().getPrescriptionMedhisDtoByIdApp(diagnoseId, new Observer<PrescriptionMedhisDto>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onNext(PrescriptionMedhisDto prescriptionMedhisDto) {
                PrescriptionMedhisDto data = prescriptionMedhisDto;
                tvHospitaName.setText(data.hospitalName);
                tvDeptName.setText("科室：" + data.dept);
                tvTime.setText("就诊时间：" + data.diagDate);
                tvIdCard.setText("就诊卡号：" + data.card);
                tvMedhiszs.setText(data.medhisZs);
                tvMedhisxbs.setText(data.medhisXbs);
                tvMedhisjws.setText(TextUtils.isEmpty(data.medhisJws) ? "无" : data.medhisJws);
                tvEyespecial.setText(data.eyeSpecial);
                tvDiagnames.setText(data.diagNames);
                tvDiagnoseadvice.setText(data.diagnoseAdvice);
                tvPatientName.setText("就诊人:" + data.name);
                tvPatientSex.setText("性别:" + data.sex);
                tvPatientAge.setText("年龄:" + data.age);
//                ImageLoaderHelper.getInstance().GlideImageLoader(mContext, "", ivSign);
//                list.clear();
//                list.addAll( data.westernNameDtoList );
//                adapter.notifyDataSetChanged();

            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError(mContext, e);
            }

            @Override
            public void onComplete() {

            }
        });
    }


    private void setStatusColor(TextView tvStatus, String status) {
        switch (status) {
            case "5":
            case "7":
            case "11":
                tvStatus.setTextColor(ContextCompat.getColor(mContext, R.color.ff999999));
                break;

            default:
                tvStatus.setTextColor(ContextCompat.getColor(mContext, R.color.fff54c4c));
        }
    }


    private String getStatus(int status) {
        switch (status) {
            case 401:
                return "待支付";
            case 402:
                return "未发货";
            case 403:
                return "已发货";
            case 404:
                return "已收货";
            case 405:
                return "待领取";
            default:
                return "";
        }
    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {

        @NotNull
        @Override
        public Holder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
            return new Holder(LayoutInflater.from(mContext).inflate(R.layout.item_photo, parent, false));
        }

        @Override
        public void onBindViewHolder(@NotNull final Holder holder, final int position) {
            if (!TextUtils.isEmpty(photoList.get(position).shareUrl)) {
                GlideUtil.loadImage(mContext, photoList.get(position).shareUrl, holder.image);
            } else if (!TextUtils.isEmpty(photoList.get(position).thumbnailUrl)) {
                GlideUtil.loadImage(mContext, photoList.get(position).thumbnailUrl, holder.image);
            }
        }

        @Override
        public int getItemCount() {
//            if (type == 1 || type == 0) {
//                return list.size() + 1;
//            }
            return photoList.size();
        }


        class Holder extends RecyclerView.ViewHolder {

            ImageView image;
            ImageView btn_del_photo;

            Holder(View itemView) {
                super(itemView);
                image = itemView.findViewById(R.id.image);
                btn_del_photo = itemView.findViewById(R.id.btn_del_photo);
            }
        }
    }
}
